package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.main.activity.PayOrderItemsActivity;
import com.zhongjian.technologycal.mall.adapter.OrdersDetailsListAdapter;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.bean.OrdersDetailsBean;
import com.zhongjian.technologycal.mall.bean.PayBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UiUtils;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.GetDialog;
import com.zhongjian.technologycal.mall.view.ListViewForScrollView;
import com.zhongjian.technologycal.mall.view.PasswordInputView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;


/**
 * Created by wei on 2017/12/22.
 * 订单详情页面的实现类
 */

public class OrderDetailsActivity extends BaseActivity {
    private Context context;
    private Dialog payDialog = null;
    GetDialog getDialog1 = new GetDialog();
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.orders_details_tv_status)
    TextView orders_details_tv_status;
    @BindView(R.id.orders_details_tv_name)
    TextView orders_details_tv_name;
    @BindView(R.id.orders_details_tv_address)
    TextView orders_details_tv_address;
    @BindView(R.id.orders_details_tv_phone)
    TextView orders_details_tv_phone;
    @BindView(R.id.orders_details_tv_ordersno)
    TextView orders_details_tv_ordersno;
    @BindView(R.id.orders_details_tv_createtime)
    TextView orders_details_tv_createtime;
    @BindView(R.id.orders_details_tv_coupon) //现金红包
            TextView orders_details_tv_coupon;
    @BindView(R.id.orders_details_tv_ElecNum)//现金币
            TextView orders_details_tv_ElecNum;
    @BindView(R.id.orders_details_tv_RemainPoints) //购物币
            TextView orders_details_tv_RemainPoints;
    @BindView(R.id.orders_details_tv_RemainVIPAmount) //消费积分
            TextView orders_details_tv_RemainVIPAmount;
    @BindView(R.id.orders_details_tv_Realpay)
    TextView orders_details_tv_Realpay;
    @BindView(R.id.orders_details_tv_total_price)
    TextView orders_details_tv_total_price;
    @BindView(R.id.orders_details_tv_money)
    TextView orders_details_tv_money;
    @BindView(R.id.tv_commit_payment)
    TextView tv_commit_payment;

    @BindView(R.id.order_details_for_list)
    ListViewForScrollView order_details_for_list;
    private OrdersDetailsListAdapter ordersDetailsListAdapter;
    private List<OrdersDetailsBean> listItems = new ArrayList<>();
    private int type = 0;
    private Dialog hintDialog = null;
    @BindView(R.id.tv_apply_drawback)
    TextView tv_apply_drawback;
    @BindView(R.id.rl_real_state)
    RelativeLayout rl_real_state;

    private int orderId;
    private String orderNo;
    private Dialog picChooseDialog = null;
    private String mContent = null;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("订单详情");
        orderId = getIntent().getIntExtra("orderId", 0);
        orderNo = getIntent().getStringExtra("orderNo");
        Log.v("orderNo", orderNo);
        initRequest();

        ordersDetailsListAdapter = new OrdersDetailsListAdapter(getBaseContext(), listItems);
        order_details_for_list.setAdapter(ordersDetailsListAdapter);

    }

    /**
     * 请求订单详情接口
     */
    private void initRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .getUrlRequestgetOrderDetails(UserUtils.getUserToken(), orderId, new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(OrderDetailsActivity.this);

                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            OrdersDetailsBean ordersDetailsBean = gson.fromJson(responseBody.string(), OrdersDetailsBean.class);
                            if (ordersDetailsBean.getError_code() == 0) {
                                //订单状态订单状态 -3=退货完成 -2=订单取消 -1=等待支付 0=已支付，等待发货 1=已发货 2=确认收货 3=评价(完成) 4=申请退货 5=申请取消
                                if (ordersDetailsBean.getData().getCurstatus() == 0) {
                                    type = 0;
                                    orders_details_tv_status.setText("已支付，等待发货");
                                    tv_commit_payment.setText("申请退款");
                                    tv_apply_drawback.setVisibility(View.GONE);
                                } else if (ordersDetailsBean.getData().getCurstatus() == 1) {
                                    type = 1;
                                    orders_details_tv_status.setText("已发货");
                                } else if (ordersDetailsBean.getData().getCurstatus() == 2) {
                                    type = 2;
                                    orders_details_tv_status.setText("确认收货");
                                    tv_commit_payment.setText("确认收货");
                                } else if (ordersDetailsBean.getData().getCurstatus() == 3) {
                                    type = 3;
                                    orders_details_tv_status.setText("评价(完成)");

                                } else if (ordersDetailsBean.getData().getCurstatus() == 4) {
                                    type = 4;
                                    orders_details_tv_status.setText("申请退货");
                                    tv_commit_payment.setText("申请退货");
                                    tv_apply_drawback.setVisibility(View.VISIBLE);
                                    tv_apply_drawback.setText("确认收货");

                                } else if (ordersDetailsBean.getData().getCurstatus() == 5) {
                                    type = 5;
                                    orders_details_tv_status.setText("已取消订单");
                                    rl_real_state.setVisibility(View.GONE);

                                } else if (ordersDetailsBean.getData().getCurstatus() == -1) {
                                    type = -1;
                                    orders_details_tv_status.setText("等待支付");
                                    tv_commit_payment.setText("立即支付");
                                    tv_apply_drawback.setText("取消订单");
                                } else if (ordersDetailsBean.getData().getCurstatus() == -2) {
                                    type = -2;
                                    orders_details_tv_status.setText("订单已取消");
                                    rl_real_state.setVisibility(View.GONE);

                                } else if (ordersDetailsBean.getData().getCurstatus() == -3) {
                                    type = -3;
                                    orders_details_tv_status.setText("退货完成");
                                }
                                String phone = ordersDetailsBean.getData().getReceiverphone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");

                                orders_details_tv_name.setText(ordersDetailsBean.getData().getReceivername());
                                orders_details_tv_phone.setText(phone);
                                orders_details_tv_address.setText(
                                        ordersDetailsBean.getData().getProvincename() +
                                                ordersDetailsBean.getData().getCityname()
                                                + ordersDetailsBean.getData().getRegionname()
                                                + ordersDetailsBean.getData().getAddress());
                                orders_details_tv_ordersno.setText(ordersDetailsBean.getData().getOrderno());
                                orders_details_tv_createtime.setText(ordersDetailsBean.getData().getCreatetime());

                                orders_details_tv_coupon.setText("" + ordersDetailsBean.getData().getUsecoupon());
                                orders_details_tv_ElecNum.setText("" + ordersDetailsBean.getData().getUseelecnum());
                                orders_details_tv_RemainPoints.setText("" + ordersDetailsBean.getData().getUsepointnum());
                                orders_details_tv_RemainVIPAmount.setText("" + ordersDetailsBean.getData().getUsevipremainnum());
                                orders_details_tv_Realpay.setText("￥" + ordersDetailsBean.getData().getRealpay());
                                orders_details_tv_total_price.setText("￥" + ordersDetailsBean.getData().getTotalamount());
                                if (ordersDetailsBean.getData().getFreight() == 0) {
                                    orders_details_tv_money.setText("包邮");
                                } else {
                                    orders_details_tv_money.setText("" + ordersDetailsBean.getData().getFreight());
                                }

                                List<OrdersDetailsBean> forList = new ArrayList<>();

                                for (int i = 0; i < ordersDetailsBean.getData().getOrderlines().size(); i++) {
                                    forList.add(ordersDetailsBean);
                                }
                                setData(forList);

                            } else if (ordersDetailsBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(ordersDetailsBean.getError_message());

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    private void setData(List<OrdersDetailsBean> foreshowList) {

        for (OrdersDetailsBean vo : foreshowList) {
            listItems.add(vo);
        }
        ordersDetailsListAdapter.notifyDataSetChanged();

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_order_details;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.tv_apply_drawback)
    public void onClickApplyDrawBack() {
        if (type == -1) {
            hintDialog = getDialog.getHintDialog(OrderDetailsActivity.this, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    requestCancelOrder(orderNo);
                    hintDialog.dismiss();
                }
            }, "确定取消订单吗?", true);
            hintDialog.show();
        } else if (type == 4) {
            hintDialog = getDialog.getHintDialog(OrderDetailsActivity.this, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    requestconfirmOrder(orderNo);
                    hintDialog.dismiss();
                }
            }, "确定收货吗？", true);
            hintDialog.show();
        }

    }

    /**
     * 提交请求
     */
    @OnClick(R.id.tv_commit_payment)
    public void onClickCommitPayment() {
        if (type == 0) {
            initshowDialog();
        } else if (type == 1) {

        } else if (type == 2) {

        } else if (type == 3) {

        } else if (type == 4) {
            initshowDialog1();
        } else if (type == 5) {

        } else if (type == -1) {
            hintDialog = getDialog1.getHintDialog(OrderDetailsActivity.this, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    requestPayOrder(orderNo);
                    hintDialog.dismiss();
                }
            }, "确定支付订单吗？", true);
            hintDialog.show();

        } else if (type == -2) {


        } else if (type == -3) {


        }

    }

    /**
     * 弹出退款对话框
     */
    private void initshowDialog() {
        picChooseDialog = new Dialog(this, R.style.pic_choose_dialog_progress);
        View view = LayoutInflater.from(getBaseContext()).inflate(
                R.layout.info_choose_dialog, null);
        final CheckBox checkbox_ch_info0 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info0);
        final CheckBox checkbox_ch_info1 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info1);
        final CheckBox checkbox_ch_info2 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info2);
        final CheckBox checkbox_ch_info3 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info3);
        final CheckBox checkbox_ch_info4 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info4);
        final CheckBox checkbox_ch_info5 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info5);
        final TextView ch_tv_order_commit = (TextView) view
                .findViewById(R.id.ch_tv_order_commit);
        checkbox_ch_info0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(true);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info0.getText().toString();
            }
        });
        checkbox_ch_info1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(true);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info1.getText().toString();
            }
        });
        checkbox_ch_info2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(true);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info2.getText().toString();
            }
        });
        checkbox_ch_info3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(true);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info3.getText().toString();
            }
        });
        checkbox_ch_info4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(true);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info4.getText().toString();
            }
        });

        checkbox_ch_info5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(true);
                mContent = checkbox_ch_info5.getText().toString();
            }
        });
        ch_tv_order_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkbox_ch_info0.isChecked()
                        || checkbox_ch_info1.isChecked()
                        || checkbox_ch_info2.isChecked()
                        || checkbox_ch_info3.isChecked()
                        || checkbox_ch_info4.isChecked()
                        || checkbox_ch_info5.isChecked()) {

                    requestApplyRefund(orderNo, mContent);

                } else {
                    ToastUitl.showShort("请先选择退款原因。");
                }
            }
        });

        Window window = picChooseDialog.getWindow();
        window.setContentView(view);
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(android.view.WindowManager.LayoutParams.MATCH_PARENT,
                android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        window.setWindowAnimations(R.style.popupWindowAnimation);
        picChooseDialog.setCanceledOnTouchOutside(true);
        picChooseDialog.show();
    }

    /**
     * 弹出退款对话框
     */
    private void initshowDialog1() {
        picChooseDialog = new Dialog(this, R.style.pic_choose_dialog_progress);
        View view = LayoutInflater.from(getBaseContext()).inflate(
                R.layout.info_choose_dialog, null);
        final CheckBox checkbox_ch_info0 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info0);
        final CheckBox checkbox_ch_info1 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info1);
        final CheckBox checkbox_ch_info2 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info2);
        final CheckBox checkbox_ch_info3 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info3);
        final CheckBox checkbox_ch_info4 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info4);
        final CheckBox checkbox_ch_info5 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info5);
        final TextView ch_tv_order_commit = (TextView) view
                .findViewById(R.id.ch_tv_order_commit);


        checkbox_ch_info0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(true);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info0.getText().toString();
            }
        });
        checkbox_ch_info1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(true);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info1.getText().toString();
            }
        });
        checkbox_ch_info2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(true);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info2.getText().toString();
            }
        });
        checkbox_ch_info3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(true);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info3.getText().toString();
            }
        });
        checkbox_ch_info4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(true);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info4.getText().toString();
            }
        });

        checkbox_ch_info5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(true);
                mContent = checkbox_ch_info5.getText().toString();
            }
        });
        ch_tv_order_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkbox_ch_info0.isChecked()
                        || checkbox_ch_info1.isChecked()
                        || checkbox_ch_info2.isChecked()
                        || checkbox_ch_info3.isChecked()
                        || checkbox_ch_info4.isChecked()
                        || checkbox_ch_info5.isChecked()) {
                    requestApplySaleReturn(orderNo, mContent, "", "", "");

                } else {
                    ToastUitl.showShort("请先选择退货原因。");
                }
            }
        });

        Window window = picChooseDialog.getWindow();
        window.setContentView(view);
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(android.view.WindowManager.LayoutParams.MATCH_PARENT,
                android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        window.setWindowAnimations(R.style.popupWindowAnimation);
        picChooseDialog.setCanceledOnTouchOutside(true);
        picChooseDialog.show();
    }

    /**
     * 取消订单
     *
     * @param orderNo
     */
    private void requestCancelOrder(String orderNo) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestCancelOrder(UserUtils.getUserToken(), orderNo,
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(OrderDetailsActivity.this);
                    }

                    @Override
                    public void onCompleted() {
                        LoadingDialog.cancelDialogForLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUitl.showShort(e.getMessage());

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {
                                ToastUitl.showShort("取消订单成功！");
                                finish();
                            } else if (baseBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

    }

    /**
     * 申请退款
     *
     * @param orderNo
     */
    private void requestApplyRefund(String orderNo, String meno) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestApplyRefund(UserUtils.getUserToken(), orderNo, meno,
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(OrderDetailsActivity.this);
                    }

                    @Override
                    public void onCompleted() {
                        LoadingDialog.cancelDialogForLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUitl.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {
                                finish();
                            } else if (baseBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

    }

    /**
     * 确认收货
     *
     * @param orderNo
     */
    private void requestconfirmOrder(String orderNo) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestconfirmOrder(UserUtils.getUserToken(),
                orderNo, new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {
                                finish();
                                ToastUitl.showShort("确认收货成功！");
                            } else if (baseBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

    }

    /**
     * 申请退货
     *
     * @param orderNo
     */
    private void requestApplySaleReturn(String orderNo, String meno, String p0, String p1, String p2) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestApplySaleReturn(UserUtils.getUserToken(),
                orderNo, meno, p0, p1, p2, new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {
                                finish();
                                ToastUitl.showShort("申请退货成功！");

                            } else if (baseBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

    }

    /**
     * 支付订单
     *
     * @param orderNo
     */
    private void requestPayOrder(final String orderNo) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestgetPayOrder(UserUtils.getUserToken(),
                orderNo, new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            Gson gson = new Gson();
                            PayBean payBean = gson.fromJson(responseBody.string(), PayBean.class);
                            if (payBean.getError_code() == 0) {

                                if (payBean.getData().getType().equals("1")) {
                                    payDialog = new Dialog(OrderDetailsActivity.this, R.style.dialog_style1);
                                    LinearLayout layout;
                                    LayoutInflater inflater = (LayoutInflater) OrderDetailsActivity.this
                                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                                    layout = (LinearLayout) inflater.inflate(R.layout.dialog_hint,
                                            null);
                                    final PasswordInputView tv_hint = (PasswordInputView) layout
                                            .findViewById(R.id.again_paypswd_pet);
                                    layout.findViewById(R.id.dialog_btn_cancel).setOnClickListener(
                                            new View.OnClickListener() {

                                                @Override
                                                public void onClick(View v) {
                                                    payDialog.dismiss();
                                                }
                                            });
                                    layout.findViewById(R.id.dialog_btn_ensure).setOnClickListener(
                                            new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                    if (TextUtils.isEmpty(tv_hint.getText().toString().trim())) {
                                                        ToastUitl.showShort("支付密码不能为空");
                                                        return;
                                                    } else if (tv_hint.getText().toString().trim().length() < 6) {
                                                        ToastUitl.showShort("请输入6位的支付密码");
                                                        return;
                                                    }
                                                    initRequestPayPassword(tv_hint.getText().toString().trim());
                                                    payDialog.dismiss();
                                                }
                                            });
                                    payDialog.setContentView(layout);
                                    payDialog.show();
                                } else if (payBean.getData().getType().equals("2")) {
                                    Intent intent = new Intent();
                                    intent.setClass(getBaseContext(), PayOrderItemsActivity.class);
                                    intent.putExtra("singData", payBean.getData().getSingData());
                                    startActivity(intent);
                                    finish();
                                } else if (payBean.getData().getType().equals("3")) {
                                    Intent intent1 = new Intent(UiUtils.getContext(), PayOrderActivity.class);
                                    intent1.putExtra("order_no", orderNo);
                                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent1);
                                    finish();
                                }

                            } else if (payBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(payBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

    }

    /**
     * 验证支付密码
     *
     * @param password
     */

    private void initRequestPayPassword(String password) {
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestPayPassword(UserUtils.getUserToken(),
                        password, new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        ToastUitl.showShort(baseBean.getError_message());
                                        finish();
                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }


                            }
                        });

    }

}
